local assets = {
	Asset("ANIM", "anim/musha_bunnyhat.zip"),
	Asset("ANIM", "anim/musha_bunnyhat_goggles.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_bunnyhat.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_bunnyhat.tex")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end

	if not inst.EnergyOn then
		if inst.components.musha_equipment.currentlevel < 10 then
			inst.components.equippable.walkspeedmult = 1.05
		elseif inst.components.musha_equipment.currentlevel < 20 then
			inst.components.equippable.walkspeedmult = 1.08
		elseif inst.components.musha_equipment.currentlevel < 30 then
			inst.components.equippable.walkspeedmult = 1.12
		else
			inst.components.equippable.walkspeedmult = 1.15
		end
	else
		if inst.components.musha_equipment.currentlevel < 30 then
			inst.components.equippable.walkspeedmult =
				1.20 + math.floor((inst.components.musha_equipment.currentlevel - 1) / 5) * 0.05
		else
			inst.components.equippable.walkspeedmult = 1.50
		end
	end

	if inst.components.musha_equipment.currentlevel < 15 then
		inst.components.insulator.insulation = TUNING.INSULATION_SMALL
	elseif inst.components.musha_equipment.currentlevel < 30 then
		inst.components.insulator.insulation = TUNING.INSULATION_MED
	else
		inst.components.insulator.insulation =
			(inst.EnergyOn and inst.components.musha_equipment:IsBroken()) and TUNING.INSULATION_LARGE * 2 or
			TUNING.INSULATION_MED * 2
	end

	inst.components.armor:SetAbsorption(
		math.max(19 + inst.components.musha_equipment.currentlevel, inst.components.musha_equipment.currentlevel * 2 - 12) *
			0.01
	)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_HAT_BUNNY ..
					" (LV%d)\n" ..
						STRINGS.MUSHA_ITEM_SPEED ..
							"(" ..
								(inst.components.equippable.walkspeedmult - 1) * 100 ..
									")\n" .. STRINGS.MUSHA_ITEM_WARMNCOOL .. "(%s)\n" .. STRINGS.MUSHA_ARMOR .. " (%d)",
				inst.components.musha_equipment.currentlevel,
				(inst.components.musha_equipment.currentlevel < 15 and "Small") or
					(inst.components.musha_equipment.currentlevel < 30 and "MED") or
					((inst.EnergyOn and inst.components.musha_equipment:IsBroken()) and "Great") or
					"Large",
				math.max(19 + inst.components.musha_equipment.currentlevel, inst.components.musha_equipment.currentlevel * 2 - 12)
			)
		)
	end
end

local function ChangeInsulation(inst)
	if TheWorld.state.issummer then
		inst.components.insulator:SetSummer()
	elseif not TheWorld.state.issummer then
		inst.components.insulator:SetWinter()
	end
end

local function Enable_Goggle(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.components.musha_equipment:IsBroken() and owner:HasTag("player") then
		if owner:GetSandstormLevel() >= TUNING.SANDSTORM_FULL_LEVEL and not owner.components.playervision:HasGoggleVision() then
			if not inst:HasTag("goggles") then
				inst:AddTag("goggles")
			end
		else
			inst:RemoveTag("goggles")
		end
	end
end

local function Energy_Switch(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.EnergyOn then
		if inst.components.musha_equipment:IsBroken() then
			return
		end

		inst.Goggle = inst:DoPeriodicTask(0.5, Enable_Goggle)
		inst.components.musha_equipment:StartConsuming()
		owner.AnimState:OverrideSymbol("swap_hat", "musha_bunnyhat_goggles", "swap_hat")
	else
		if inst.Goggle then
			inst.Goggle:Cancel()
			inst.Goggle = nil
		end
		inst.components.musha_equipment:StopConsuming()
		owner.AnimState:OverrideSymbol("swap_hat", "musha_bunnyhat", "swap_hat")
	end
	inst.EnergyOn = not inst.EnergyOn
	Updata(inst)
	inst.components.useableitem:StopUsingItem()
end

local function OnEquip(inst, owner)
	ChangeInsulation(inst)
	inst:WatchWorldState("season", ChangeInsulation)

	owner.AnimState:OverrideSymbol("swap_hat", "musha_bunnyhat", "swap_hat")

	owner.AnimState:Hide("HAIR")
	owner.AnimState:Hide("HAIR_NOHAT")
	owner.AnimState:Show("HAT_HAIR")
	owner.AnimState:Show("HAT")

	Updata(inst)
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end

	owner.AnimState:Hide("HAT")
	owner.AnimState:Hide("HAT_HAIR")
	owner.AnimState:Show("HAIR_NOHAT")
	owner.AnimState:Show("HAIR")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst:AddTag("musha_items")

	inst.AnimState:SetBank("beehat")
	inst.AnimState:SetBuild("musha_bunnyhat")
	inst.AnimState:PlayAnimation("anim")

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("hat", 450, 0.027)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_hat", "musha_bunnyhat", "swap_hat")
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.equippable.walkspeedmult = 1
				inst.components.talker:Say(
					STRINGS.MUSHA_HAT_BROKEN .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_bunnyhat.xml"

	inst:AddComponent("insulator")
	inst.EnergyOn = false
	inst.type = "none"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end

return Prefab("musha_bunnyhat", fn, assets)
